package com.qianli.cashmama.trade.openApi.job.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fqgj.common.utils.StringUtils;
import com.qianli.cashmama.trade.common.utils.HttpUtil;
import com.qianli.cashmama.trade.common.utils.TradeConfigUtil;
import com.qianli.cashmama.trade.openApi.job.vo.ClOrderContactsRequest;
import com.qianli.cashmama.trade.openApi.job.vo.ClOrderReceiveRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * Created with IntelliJ IDEA.
 * User: sm
 * Date: 2017/7/24
 * Time: 下午7:11
 */
@Service
public class CollectionIntegrationServiceImpl implements CollectionIntegrationService {
    private static final Logger LOGGER = LoggerFactory.getLogger(CollectionIntegrationServiceImpl.class);


    @Autowired
    private TradeConfigUtil tradeConfigUtil;

    @Override
    public boolean pushToCollection(ClOrderReceiveRequest request) {

        try {
            String adminUrl = "http://147.139.66.58:9108/";
            String pushToCollectionApi = adminUrl + "collection/receive/order";
            Map<String, String> headers = new HashMap<>();
            headers.put("Content-Type", "application/json");

            String requestBody = JSON.toJSONString(request);
            String response = HttpUtil.postPageBody(pushToCollectionApi, headers, requestBody);
            LOGGER.info("pushToCollection推单给催收,api:{},request:{},response:{}", pushToCollectionApi, requestBody, JSON.toJSONString(response));

            JSONObject responseObject = JSON.parseObject(response);
            if (responseObject != null && responseObject.getIntValue("code") == 0) {
                return true;
            } else {
                LOGGER.error("pushToCollection异常,response:{}", JSON.toJSONString(response));
                return false;
            }
        } catch (Exception e) {
            LOGGER.error("逾期推送到催收失败,collectionDto:{}", JSON.toJSONString(request), e);
            return false;
        }
    }

    /**
     * 推送订单的通讯录到催收模块
     *
     * @param request
     */
    @Override
    public boolean pushContactsToCollection(ClOrderContactsRequest request) {
        try {
            String adminUrl = "http://147.139.66.58:9108/";
            String pushToCollectionApi = adminUrl + "collection/receive/contacts";
            Map<String, String> headers = new HashMap<>();
            headers.put("Content-Type", "application/json");

            String requestBody = JSON.toJSONString(request);
            LOGGER.info("pushContactsToCollection开始推通讯录给催收,request:{}", requestBody);
            String response = HttpUtil.postPageBody(pushToCollectionApi, headers, requestBody);
            LOGGER.info("pushContactsToCollection推通讯录给催收结束,response:{}", JSON.toJSONString(response));

            JSONObject responseObject = JSON.parseObject(response);
            if (responseObject != null && responseObject.getIntValue("code") == 0) {
                return true;
            } else {
                LOGGER.error("pushContactsToCollection异常,response:{}", JSON.toJSONString(response));
                return false;
            }
        } catch (Exception e) {
            LOGGER.error("通讯录推送到催收失败,collectionDto:{}", JSON.toJSONString(request), e);
            return false;
        }
    }


}
